package com.nondo.rdp.finance.endpoint.api.app.user;

import com.nondo.rdp.auth.common.UserUtils;
import com.nondo.rdp.auth.domain.User;
import com.nondo.rdp.auth.endpoint.model.ConvertCall;
import com.nondo.rdp.auth.endpoint.model.Page;
import com.nondo.rdp.auth.endpoint.model.Query;
import com.nondo.rdp.core.common.BeanUtils;
import com.nondo.rdp.core.common.CommUtil;
import com.nondo.rdp.core.model.View;
import com.nondo.rdp.finance.domain.analysis.AnalysisCollection;
import com.nondo.rdp.finance.model.analysis.AnalysisCollectionBean;
import com.nondo.rdp.finance.service.analysis.AnalysisCollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@RestController("apiAppUserFollow")
@RequestMapping("/api/app/user/analysis/follow")
public class UserFollowEndpoint {
    @Autowired
    AnalysisCollectionService analysisCollectionService;

    /**
     * 获取分析收藏列表
     * @param request
     * @param response
     * @param query
     * @return
     */
    @RequestMapping("/list")
    public View<Page<AnalysisCollectionBean>> list(HttpServletRequest request, HttpServletResponse response, @RequestBody Query<AnalysisCollection> query) {
        View<Page<AnalysisCollectionBean>> view = new View(20000, "获取列表成功");

        User user = UserUtils.getUser(request, response);

        AnalysisCollection analysis = query.getData();

        analysis.setUser(user);

        Page<AnalysisCollection> page = analysisCollectionService.findAll(new Page<AnalysisCollection>(query.getPage(), query.getSize()), analysis);

        Page<AnalysisCollectionBean> resultPage = page.convertPage(new ConvertCall<AnalysisCollection, Page<AnalysisCollectionBean>>() {
            @Override
            public Page<AnalysisCollectionBean> execute(Page<AnalysisCollection> srcPage) {
                Page<AnalysisCollectionBean> analysisBeanPage = new Page<AnalysisCollectionBean>(srcPage.getPageNumber(), srcPage.getPageSize(), srcPage.getTotalPages(), srcPage.getTotalElements());
                //处理数据
                List<AnalysisCollectionBean> list = new ArrayList<AnalysisCollectionBean>();
                for (Object item : srcPage.getContent()) {
                    AnalysisCollectionBean analysisBean = new AnalysisCollectionBean();
                    BeanUtils.copy(item, analysisBean);
                    //处理返回日期
                    analysisBean.setImage(CommUtil.getFileUrl(analysisBean.getImage()));
                    list.add(analysisBean);
                }
                analysisBeanPage.setContent(list);

                return analysisBeanPage;
            }
        });

        view.setData(resultPage);

        return view;
    }
}
